package org.redr.widgetGUI.core;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.redr.widgetGUI.settings.WidgetSettings;
import org.redr.widgetGUI.settings.WidgetState;

public class RFormulaEntry extends WidgetState{
	private Button andButton;
	private Button colonButton;
	private Button starButton;
	private Button clearButton;
	private ListBox	elementsListBox;
	private WidgetBox extrasBox;
	private ComboBox outcomeVariable;
	private LineEdit modelLineEdit;
	private WidgetLabel label;
	
	public RFormulaEntry(LayoutState parent, WidgetSettings ws) {
		super(parent, ws);
		GroupBox box = new GroupBox(parent, (WidgetSettings) ws.getSettings("box")
				.setSetting("label", ws.getSetting("label")));
		GroupBox formulaCommands	=	new GroupBox(box, (WidgetSettings) ws.getSettings("formulaCommands")
				.setSetting("label", "Formula Commands"));
		this.andButton	=	new Button(formulaCommands, (WidgetSettings) ws.getSettings("andButton")
				.setSetting("label", "And (+)"));
		this.andButton.widget().clicked.connect(this, "plusButtonCliked()");
		this.colonButton	=	new Button(formulaCommands, (WidgetSettings) ws.getSettings("colonButton")
				.setSetting("label", "Interacting With (:)"));
		this.colonButton.widget().clicked.connect(this, "colonButtonClicked()");
		this.starButton	=	new Button(formulaCommands, (WidgetSettings) ws.getSettings("starButton")
				.setSetting("label", "Together and Interacting (*)"));
		this.starButton.widget().clicked.connect(this, "starButtonClicked()");
		this.clearButton	=	new Button(formulaCommands, (WidgetSettings) ws.getSettings("clearButton")
				.setSetting("label", "clearFormula"));
		this.clearButton.widget().clicked.connect(this, "clearFormula()");
		
		this.elementsListBox	=	new ListBox(box, (WidgetSettings) ws.getSettings("elementsListBox")
				.setSetting("label", "Elements"));
		
		GroupBox modelBox	=	new GroupBox(box, (WidgetSettings) ws.getSettings("modelBox")
				.setSetting("label", "Model Formula"));
		this.extrasBox	=	new WidgetBox(modelBox, (WidgetSettings) ws.getSettings("extrasBox")
				.setSetting("label", "Extras"));
		this.outcomeVariable	=	new ComboBox(modelBox, (WidgetSettings) ws.getSettings("outcomeVariable")
				.setSetting("label", "Outcome Variable"));
		this.label = new WidgetLabel(modelBox, (WidgetSettings) ws.getSettings("equalsLabel")
				.setSetting("label", " = "));
        this.modelLineEdit = new LineEdit(modelBox, (WidgetSettings) ws.getSettings("modelLineEdit").setSetting("label", "Model"));
         
		
	}
	
	public void addItems(List<String> items){
		Map<String, String> newItems	=	new HashMap<String, String>();
		for (String i : items){
			newItems.put(i, i);
		}
	}
	
	public void addItems(Map<String, String> items){
		this.clearFormula();
        this.elementsListBox.clear();
        this.outcomeVariable.addItem("");
        for (String k : items.keySet()){
        	this.outcomeVariable.addItem(k, items.get(k));
        }
        this.elementsListBox.addItems(items);
	}
	
	public void clear(){
		this.elementsListBox.clear();
        this.outcomeVariable.clear();
        this.clearFormula();
	}
	public void clearFormula(){
		this.modelLineEdit.clear();
        this.updateEnabled(true);
	}
	
	public void colonButtonClicked(){
		this.modelLineEdit.setText(this.modelLineEdit.text() + " : ");
		this.updateEnabled(true);
	}
	
	public void starButtonClicked(){
		this.modelLineEdit.setText(this.modelLineEdit.text() + " * ");
	}
	public void plusButtonClicked(){
		this.modelLineEdit.setText(this.modelLineEdit.text() + " + ");
	}
	public void formulaButtonClicked(){
		// TODO
	}
	
	public void updateEnabled(boolean pos){
        this.elementsListBox.setEnabled(pos);
        this.andButton.setEnabled(!pos);
        this.colonButton.setEnabled(!pos);
        this.starButton.setEnabled(!pos);
	}
	
    public void FormulaEntryElementSelected(){
        this.modelLineEdit.setText(this.modelLineEdit.text() + this.elementsListBox.selectedTexts()[0]);
        this.updateEnabled(false);
    }
    
    /**
     * Returns the formula entered into this formula entry as an array of length 2.  The first is the outcome
     * variable and the second is the model.
     * 
     * @return String[2]
     */
    public String[] Formula(){
    	return new String[] {this.outcomeVariable.currentText(), this.modelLineEdit.text()};
    }
    
    public void update(Map<String, String> items){
    	this.outcomeVariable.update(items);
    	this.elementsListBox.update(items);
    }

}
